package com.xiao.rabbitmq.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //设置一个工厂类，进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.94.131");
        connectionFactory.setPort(5672);
        //设置虚拟主机
        connectionFactory.setVirtualHost("/");
        connectionFactory.setConnectionTimeout(60000);
        //创建一个连接
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        String queueName = "test001";
        //队列名称，是否持久化, , 如果与管道失去关联则删除
        channel.queueDeclare(queueName, true,false,false, null);
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        //设置channel， 队列，是否ack会produce， 
        channel.basicConsume(queueName, true,queueingConsumer);
        System.out.println("开始消费数据");
        while (true) {
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String string = new String(delivery.getBody());
            System.out.println(string);
        }
    }
}
